<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>user_authorized_for? (RoleRequirement::RoleSecurityClassMethods)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File lib/role_requirement_system.rb, line 57</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">user_authorized_for?</span>(<span class="ruby-identifier">user</span>, <span class="ruby-identifier">params</span> = {}, <span class="ruby-identifier">binding</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">binding</span>)
      <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">Array</span><span class="ruby-operator">===</span><span class="ruby-ivar">@role_requirements</span>
      <span class="ruby-ivar">@role_requirements</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span> <span class="ruby-identifier">role_requirement</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">roles</span> = <span class="ruby-identifier">role_requirement</span>[<span class="ruby-identifier">:roles</span>]
        <span class="ruby-identifier">options</span> = <span class="ruby-identifier">role_requirement</span>[<span class="ruby-identifier">:options</span>]
        <span class="ruby-comment cmt"># do the options match the params?</span>
        
        <span class="ruby-comment cmt"># check the action</span>
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:only</span>)
          <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:only</span>].<span class="ruby-identifier">include?</span>( (<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:action</span>]<span class="ruby-operator">||</span><span class="ruby-value str">&quot;index&quot;</span>).<span class="ruby-identifier">to_sym</span> )
        <span class="ruby-keyword kw">end</span>
        
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:except</span>)
          <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:except</span>].<span class="ruby-identifier">include?</span>( (<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:action</span>]<span class="ruby-operator">||</span><span class="ruby-value str">&quot;index&quot;</span>).<span class="ruby-identifier">to_sym</span>)
        <span class="ruby-keyword kw">end</span>
        
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:if</span>)
          <span class="ruby-comment cmt"># execute the proc.  if the procedure returns false, we don't need to authenticate these roles</span>
          <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> ( <span class="ruby-constant">String</span><span class="ruby-operator">===</span><span class="ruby-identifier">options</span>[<span class="ruby-identifier">:if</span>] <span class="ruby-operator">?</span> <span class="ruby-identifier">eval</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:if</span>], <span class="ruby-identifier">binding</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:if</span>].<span class="ruby-identifier">call</span>(<span class="ruby-identifier">params</span>) )
        <span class="ruby-keyword kw">end</span>
        
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:unless</span>)
          <span class="ruby-comment cmt"># execute the proc.  if the procedure returns true, we don't need to authenticate these roles</span>
          <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">if</span> ( <span class="ruby-constant">String</span><span class="ruby-operator">===</span><span class="ruby-identifier">options</span>[<span class="ruby-identifier">:unless</span>] <span class="ruby-operator">?</span> <span class="ruby-identifier">eval</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:unless</span>], <span class="ruby-identifier">binding</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:unless</span>].<span class="ruby-identifier">call</span>(<span class="ruby-identifier">params</span>) )
        <span class="ruby-keyword kw">end</span>
        
        <span class="ruby-comment cmt"># check to see if they have one of the required roles</span>
        <span class="ruby-identifier">passed</span> = <span class="ruby-keyword kw">false</span>
        <span class="ruby-identifier">roles</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">role</span><span class="ruby-operator">|</span>
          <span class="ruby-identifier">passed</span> = <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">user</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">user</span>.<span class="ruby-identifier">has_role?</span>(<span class="ruby-identifier">role</span>)
        }
        <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">false</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">passed</span>
      }
      
      <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">true</span>
    <span class="ruby-keyword kw">end</span></pre>
</body>
</html>